package com.gyg.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyg.common.dto.LoginDto;
import com.gyg.common.lang.Result;
import com.gyg.entity.User;
import com.gyg.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.omg.CORBA.UnknownUserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;



@RestController
public class AccountController {

    @Autowired
    UserService userService;

    @PostMapping("/login")
    public Result login(@Validated @RequestBody LoginDto loginDto) {
        /**
         User user = userService.getOne(new QueryWrapper<User>().eq("username", loginDto.getUsername()));
         //        断言判断用户是否存在
         Assert.notNull(user,"用户不存在");

         //        判断密码是否正确
         //        对密码进行MD5加密
         String md5 = SecureUtil.md5(loginDto.getPassword());
         if(user.getPassword().equals(loginDto.getPassword())){
         return Result.fail("密码不正确");
         }
         **/

        System.out.println("用户名和密码:" + loginDto.getUsername() + " " + loginDto.getPassword());

//        获取到当前用户
        Subject subject = SecurityUtils.getSubject();
//        封装用户名和密码
        UsernamePasswordToken token = new UsernamePasswordToken(loginDto.getUsername(), loginDto.getPassword());

        try {
//            使用shiro进行用户验证
            subject.login(token);
//            如果验证通过再根据用户名查找到该用户
            User user = userService.getOne(new QueryWrapper<User>().eq("username", loginDto.getUsername()));
            
            if (!user.getPassword().equals(loginDto.getPassword())){
                return Result.fail("密码错误！");
            }
            
            //            如果正确就返回用户信息
            return Result.success(MapUtil.builder()
                    .put("id", user.getId())
                    .put("username", user.getUsername())
                    .put("avatar", user.getAvatar())
                    .put("email", user.getEmail())
                    .map()
            );
        } catch (UnknownAccountException e) {
            return Result.fail("用户不存在");
        } catch (IncorrectCredentialsException e) {
            return Result.fail("密码不正确");
        }

    }

    /**
     * 退出登录
     * @return
     */
    @GetMapping("/logout")
    public Result logout(){
        Subject subject = SecurityUtils.getSubject();
//        会请求到logout
        subject.logout();
        
        return Result.success("退出成功");
    }


}
